/********************************************************************
 Software License Agreement:

 The software supplied herewith by Microchip Technology Incorporated
 (the "Company") for its PIC(R) Microcontroller is intended and
 supplied to you, the Company's customer, for use solely and
 exclusively on Microchip PIC Microcontroller products. The
 software is owned by the Company and/or its supplier, and is
 protected under applicable copyright laws. All rights are reserved.
 Any use in violation of the foregoing restrictions may subject the
 user to criminal sanctions under applicable laws, as well as to
 civil liability for the breach of the terms and conditions of this
 license.

 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *******************************************************************/

#ifndef SYSTEM_H
#define SYSTEM_H

#include <p24FJ64GB002.h>
#include <stdbool.h>

#include "buttons.h"
#include <leds.h>
#include <adc.h>

#include "mem_sst25vf064c.h"

#include <power.h>

#define MAIN_RETURN int

//#define log(...) {char s[80]; sprintf(s, __VA_ARGS__); println(s);}
#define log(...) {};

extern unsigned long hres;
extern unsigned long long ts;

#define LED1_LAT      LATBbits.LATB0
#define LED1_TRIS     TRISBbits.TRISB0

#define LED2_LAT    LATBbits.LATB1
#define LED2_TRIS   TRISBbits.TRISB1

#define ADC_CS_TRIS   TRISBbits.TRISB8
#define ADC_CS        LATBbits.LATB8

#define MEM_CS_TRIS   TRISBbits.TRISB9
#define MEM_CS        PORTBbits.RB9

// important: SDI is marked as SDO on the schematic
#define SDI_TRIS  TRISBbits.TRISB14
#define SDI       PORTBbits.RB14

// important: SDO is marked as SDI on the schematic
#define SDO_TRIS  TRISBbits.TRISB13
#define SDO       PORTBbits.RB13

#define PWROPAMP_TRIS  TRISBbits.TRISB15   // powering external op amps
#define PWROPAMP       PORTBbits.RB15

#define PWR5V_TRIS  TRISAbits.TRISA3
#define PWR5V_NEG   PORTAbits.RA3
#define PWR5V_ODC   ODCAbits.ODA3

//#define FCY 2000000
extern unsigned long fcy;

#define USE_USB_BUS_SENSE_IO 1
#define USB_BUS_SENSE_TRIS TRISBbits.TRISB7
#define USB_BUS_SENSE PORTBbits.RB7

#define self_power 1

#define DT 8
#define D 10

void __delay_us(unsigned long d);

extern unsigned char timestamp[8];
void print(char *s);
void println(char *s);
void GetTimeStamp();
void PowerFlash(bool b);
void PowerOpamp(bool b);
void InitPorts();
void InitSPI(unsigned char spre, unsigned char ppre);
void InitT1();
void TriggerT2(unsigned long i);
void InitUART2(unsigned long rate, unsigned long fcy);
void USBOn();
void USBOff();
void USBStuff();
void SetRTC();
void FastClock();
void SlowClock();

/*** System States **************************************************/
typedef enum
{
    SYSTEM_STATE_USB_START,
    SYSTEM_STATE_USB_SUSPEND,
    SYSTEM_STATE_USB_RESUME
} SYSTEM_STATE;

/*********************************************************************
* Function: void SYSTEM_Initialize( SYSTEM_STATE state )
*
* Overview: Initializes the system.
*
* PreCondition: None
*
* Input:  SYSTEM_STATE - the state to initialize the system into
*
* Output: None
*
********************************************************************/
void SYSTEM_Initialize( SYSTEM_STATE state );


/*********************************************************************
* Function: void SYSTEM_Tasks(void)
*
* Overview: Runs system level tasks that keep the system running
*
* PreCondition: System has been initalized with SYSTEM_Initialize()
*
* Input: None
*
* Output: None
*
********************************************************************/
//void SYSTEM_Tasks(void);
#define SYSTEM_Tasks()

#endif //SYSTEM_H
